/*
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dailydev.wave.calendar;

import static com.google.wave.api.EventType.*;

import java.io.UnsupportedEncodingException;

import org.dailydev.wave.robot.EventHandlerOperation;
import org.dailydev.wave.robot.filter.ButtonPressed;

import com.google.wave.api.Blip;
import com.google.wave.api.Context;
import com.google.wave.api.ElementType;
import com.google.wave.api.Event;
import com.google.wave.api.FormElement;
import com.google.wave.api.FormView;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.Wavelet;

/**
 * @author Roman Bosak, DailyDev.org
 */
public class FormRobotHandler {

    /**
     * @param event
     * @param bundle
     * @throws UnsupportedEncodingException
     */
    @EventHandlerOperation(eventTypes = { BLIP_SUBMITTED }, contexts = { Context.PARENT })
    public void handleDateInDocument(Event event, RobotMessageBundle bundle) throws UnsupportedEncodingException {
        Blip blip = event.getBlip();
        Blip child = blip.createChild();

        FormView formView = child.getDocument().getFormView();
        formView.append(new FormElement(ElementType.INPUT, "Input", "input", "Write something"));
        formView.append(new FormElement(ElementType.BUTTON, "Submit", "submit", "submitValue"));
        formView.append(new FormElement(ElementType.BUTTON, "Cancel", "cancel", "cancelValue"));
    }

    @EventHandlerOperation(eventTypes = { FORM_BUTTON_CLICKED })
    @ButtonPressed("submit")
    public void handleSubmitButtonPressed(Event event, Wavelet wavelet) throws UnsupportedEncodingException {
        Blip blip = event.getBlip();
        blip.getDocument().append("\nSubmit pressed");
    }

    @EventHandlerOperation(eventTypes = { FORM_BUTTON_CLICKED })
    @ButtonPressed("cancel")
    public void handleCancelButtonPressed(Event event, Wavelet wavelet) throws UnsupportedEncodingException {
        Blip blip = event.getBlip();
        blip.getDocument().append("\nCancel pressed");
    }
}
